import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.security.MessageDigest;

public class Hash256 {


    public static byte[] hash(String content) {
        try {
            MessageDigest object = MessageDigest.getInstance("SHA-256");
            byte[] encrypted = object.digest(content.getBytes("UTF-8"));
            return encrypted;
        } catch (Exception e) {

        }

        return null;
    }


    public static byte[] hash(byte[] content) {
        try {
            MessageDigest object = MessageDigest.getInstance("SHA-256");
            return object.digest(content);
        } catch (Exception ignored) {

        }

        throw new Error("SHA-256-ERROR");
    }


    public static void main(String[] args) throws Exception {

        String tx1 = "coinbase_A_50";
        // coinbase朝A转账50 这是A工作的动力
        String tx2 = "B_C_1";
        //   B朝C转账1
        String tx3 = "B_D_1";
        //   B朝D转账1

        // 这个计算量很大
        for (long i = 1; i < Long.MAX_VALUE; i++) {
            String nonce = String.valueOf(i);
            String content = tx1 + tx2 + tx3 + nonce;
            byte[] encrypted = Hash256.hash(content);
            if (encrypted[0] == 0 && encrypted[1] == 0) {
                System.out.println(nonce);
                break;
            }
        }
    }


    public static int nbit(byte[] data) {

        assert data.length == 32;
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(data);
        DataInputStream dataInputStream = new DataInputStream(byteInputStream);

        int count = 0;

        try {
            for (int j = 0; j < 8; j++) {
                int i = dataInputStream.readInt();
                int leadingZeros = Integer.numberOfLeadingZeros(i);
                count += leadingZeros;
                if (leadingZeros < 32) {
                    break;
                }
            }
        } catch (Exception ignored) {

        }
        return count;
    }

}